package reptile

import (
	"bufio"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"robot/study/LiveRobot/define"
	"robot/study/LiveRobot/sdk/zlog"
	"strconv"
	"strings"
)

type Response struct {
	Flag bool
	U    *define.Message2
}

var Res *Response

//智能回答
func (resa *Response) Work() {
	var s string
	if strings.Contains(resa.U.RawMessage, "[CQ:image") {
		return
	}
	if strings.Contains(resa.U.RawMessage, "你好") {
		s = resa.U.Sender.Nickname + "好" + "[CQ:at,qq=" + strconv.FormatInt(resa.U.UserId, 10) + "]"
	} else {
		var res define.Response
		resp, err := http.Get("http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + resa.U.RawMessage)
		if err != nil {
			zlog.Error("智能回答失败", err)
		}
		body := bufio.NewReader(resp.Body)
		b, _ := ioutil.ReadAll(body)
		json.Unmarshal(b, &res)
		//s = res.Content + "[CQ:at,qq=" + strconv.FormatInt(resa.U.UserId, 10) + "]"
		s = res.Content
	}
	mes := &define.Msg{
		Msg_type: "group",
		Number:   define.QUN,
		Msg:      s,
	}
	Sendmsg(mes)
}

func (resa *Response) ResponseOff() {
	Res.Flag = false
	mes := &define.Msg{
		Msg_type: "group",
		Number:   define.QUN,
		Msg:      "已关闭智能回答",
	}
	Sendmsg(mes)
}

func (resa *Response) ResponseOpen() {
	Res.Flag = true
	mes := &define.Msg{
		Msg_type: "group",
		Number:   define.QUN,
		Msg:      "已开启智能回答",
	}
	Sendmsg(mes)
}
